package com.woniuxy.controller;


import com.woniuxy.config.Sample;
import com.woniuxy.pojo.StopCar;
import com.woniuxy.service.StopCarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.sound.midi.Soundbank;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ai")
@CrossOrigin(allowCredentials="true",maxAge = 3600)
@PropertySource(value = "classpath:config/global.properties")
@Api(tags = "百度AI类")
public class BaiDuAiController {

    @Autowired
    private Sample sample;

    @Autowired
    private StopCarService stopCarService;

    //文件存储路径的根目录
    @Value("${upfile.path}")
    private String root;


    @PostMapping("/scan")
    @ResponseBody
    @ApiOperation(value = "返回上传图片是否为车牌图片")
    @ApiImplicitParam(name = "image",value = "图片地址",required = true,dataType = "String")
    public Map aiEwm(String image){
        Map<String,Object> map = new HashMap<>();
        System.out.println(image+"?????????????????");
        Map result = this.sample.sample(image);
        if (result.size() == 2){
            map.put("code",200);
            map.put("result",result);
        }else {
            map.put("code",500);
            map.put("result",result);
        }

        return map;
    }

    //已停车辆页面所需参数
    @GetMapping("/allCar")
    @ResponseBody
    @ApiOperation(value = "已停车辆页面所需参数")
    public Map allCar(){
        Map<String,Object> map = new HashMap<>();
        List<StopCar> lists = stopCarService.findAllCar();
        map.put("code",0);
        map.put("msg","");
        map.put("count",null);
        map.put("data",lists);
        return map;
    }


    //所有车辆停车记录页面所需参数
    @GetMapping("/ezstop")
    @ResponseBody
    @ApiOperation(value = "所有车辆停车记录页面所需参数")
    public Map ezstop(){
        Map<String,Object> map = new HashMap<>();
        List<StopCar> lists = stopCarService.findEzstopCar();
        map.put("code",0);
        map.put("msg","");
        map.put("count",null);
        map.put("data",lists);
        return map;
    }


    //上传文件
    @PostMapping("/uploadCarPhoto")
    @ResponseBody
    @ApiOperation(value = "图片上传")
    @ApiImplicitParam(name = "file",value = "MultipartFile",required = true,dataType = "MultipartFile")
    public Map upload(MultipartFile file){
        Map map = new HashMap<>();
        //文件名
        String fileName = new SimpleDateFormat("yyyyMMddhhmmsss").format(new Date());
        //文件的扩展名  hello.jpg
        String  ext = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //归档
        String filePath = root+ File.separator+fileName+ext;
        try {
            file.transferTo(new File(filePath));

            //向客户端返回文件的路径  src==>  /carousel/img/hello.jpg

            map.put("code",200);
            map.put("msg","上传成功！");
            map.put("src",root+File.separator+fileName+ext);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","上传失败！");
        }
        return map;
    }


    //停车
    @PostMapping("/stopcar")
    @ResponseBody
    @ApiOperation(value = "停车方法")
    @ApiImplicitParam(name = "carnumber",value = "车牌号",required = true,dataType = "String")
    public Map stopcar(String carnumber){
        System.out.println(carnumber+"?????????????????/////////////////////////");
        Map map = new HashMap();
        //查询当前车辆是否在小区内
        StopCar sc = stopCarService.findCarByCarnumber(carnumber);
        if (sc != null){
            map.put("code",500);
            map.put("msg","该车辆已在小区内");
        }else {
            try {
                stopCarService.stopcar(carnumber);
                map.put("code",200);
                map.put("msg","停车成功！");
            }catch (Exception e){
                e.printStackTrace();
                map.put("code",500);
                map.put("msg","系统错误");
            }
        }
        return map;
    }


    //离开小区
    @PostMapping("/leave")
    @ResponseBody
    @ApiOperation(value = "车辆离开小区")
    @ApiImplicitParam(name = "id",value = "停车表id",required = true,dataType = "Long")
    public Map leave(Long id){
        Map map = new HashMap();
        //查询小区内是否有该车辆
        StopCar sc = stopCarService.findCarById(id);

        if(sc != null){
            stopCarService.updateStatusById(id);
            map.put("code",200);
            map.put("msg","车辆驶出小区。。。");
        }else {
            map.put("code",500);
            map.put("msg","小区内不存在该车辆！");
        }
        return map;
    }

}
